﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Baula.TextGen.Framework.Tests
{
    [TestClass]
    public class RealWorldTests
    {
        [TestMethod]
        [TestCategory("Acceptance Test")]
        public void VB6CodeGeneration_SimpleClassGenerationWithListOfProperties_WorksOutCorrectly()
        {
            const string templateContent =
                @"Option Explicit
Option Compare Database
' This is %ModuleName%

' %Section:Declarations%

Public Sub Class_Initialize()
    ' %Section:Initialization%
End Sub

Public Sub Class_Terminate()
    ' %Section:Cleanup%
End Sub
' === ValueTypeProperty

Public Property Let %Name%(ByVal New%Name% As %Type%)
    mp_%Name% = New%Name%
End Property
Public Property Get %Name% As %Type%
    %Name% = mp_%Name%
End Property
' @Section:Declarations
Private mp_%Name% As %Type%
    ' @Section:Initialization
    mp_%Name% = %InitializationExpression%
' === ReferenceTypeProperty

' @Section:Declarations
Private mp_%Name% As %Type%
    ' @Section:Initialization
    Set mp_%Name% = %InitializationExpression%
    ' @Section:Cleanup
    Set mp_%Name% = Nothing
' @Section:End
Public Property Set %Name%(ByVal New%Name% As %Type%)
    Set mp_%Name% = New%Name%
End Property
Public Property Get %Name% As %Type%
    Set %Name% = mp_%Name%
End Property";
            const string expectedCode =
                @"Option Explicit
Option Compare Database
' This is BL_Person

' %Section:Declarations%
Private mp_ID As Long
Private mp_FirstName As String
Private mp_LastName As String
Private mp_DateOfBirth As Date
Private mp_Children As VBA.Collection

Public Sub Class_Initialize()
    ' %Section:Initialization%
    mp_ID = 0
    mp_FirstName = """"
    mp_LastName = """"
    mp_DateOfBirth = CDate(0)
    Set mp_Children = New VBA.Collection
End Sub

Public Sub Class_Terminate()
    ' %Section:Cleanup%
    Set mp_Children = Nothing
End Sub

Public Property Let ID(ByVal NewID As Long)
    mp_ID = NewID
End Property
Public Property Get ID As Long
    ID = mp_ID
End Property

Public Property Let FirstName(ByVal NewFirstName As String)
    mp_FirstName = NewFirstName
End Property
Public Property Get FirstName As String
    FirstName = mp_FirstName
End Property

Public Property Let LastName(ByVal NewLastName As String)
    mp_LastName = NewLastName
End Property
Public Property Get LastName As String
    LastName = mp_LastName
End Property

Public Property Let DateOfBirth(ByVal NewDateOfBirth As Date)
    mp_DateOfBirth = NewDateOfBirth
End Property
Public Property Get DateOfBirth As Date
    DateOfBirth = mp_DateOfBirth
End Property

Public Property Set Children(ByVal NewChildren As VBA.Collection)
    Set mp_Children = NewChildren
End Property
Public Property Get Children As VBA.Collection
    Set Children = mp_Children
End Property";
            var properties = new[] {
                                       new {Name = "ID", Type = "Long", InitializatioExpression = "0", IsReferenceType = false},
                                       new {Name = "FirstName", Type = "String", InitializatioExpression = "\"\"", IsReferenceType = false},
                                       new {Name = "LastName", Type = "String", InitializatioExpression = "\"\"", IsReferenceType = false},
                                       new {Name = "DateOfBirth", Type = "Date", InitializatioExpression = "CDate(0)", IsReferenceType = false},
                                       new {Name = "Children", Type = "VBA.Collection", InitializatioExpression = "New VBA.Collection", IsReferenceType = true}
                                   };
            var documentTemplate = new DocumentTemplate(templateContent);
            var document = documentTemplate.CreateDocument();
            document.ValuesForPlaceholders["ModuleName"] = "BL_Person";

            foreach (var property in properties)
            {
                var partTemplateToUse = (property.IsReferenceType)
                                            ? documentTemplate.PartTemplates["ReferenceTypeProperty"]
                                            : documentTemplate.PartTemplates["ValueTypeProperty"];
                var part = partTemplateToUse.CreatePart();

                part.ValuesForPlaceholders["Name"] = property.Name;
                part.ValuesForPlaceholders["Type"] = property.Type;
                part.ValuesForPlaceholders["InitializationExpression"] = property.InitializatioExpression;

                document.Parts.Add(part);
            }

            var actualCode = document.RenderToString();

            Assert.AreEqual(expectedCode, actualCode);
        }
    }
}
